Read Only Overlay

Introduction

Whilst an operating system is running the programs it runs will occasionally write data to the file system. If power is removed from the Raspberry Pi (or any computer for that matter) whilst it is running, it is possible that the file systems in use by the operating system may become corrupted. In addition SD cards have a limited life span in terms of the number of writes they can perform.

To protect our SD card from corruption and to maximise its lifespan we can use overlayfs to create a writeable “layer” for the OS to use (in memory) on top of the actual file system which is mounted read-only.

Installation

Nils Werner has created a handy Arch Linux package to create and manage the overlay. It can be found at: https://github.com/nils-werner/raspi-overlayroot

Note

To make Arch Linux packages you must run the makepkg command as a non-root user.

Install required packages:

pacman -S fakeroot sudo git arch-install-scripts

Build package and install package:

su - neil
git clone https://github.com/nils-werner/raspi-overlayroot
cd raspi-overlayroot
makepkg
exit
pacman -S ~neil/raspi-overlayroot/overlayroot-0.2-1-any.pkg.tar.xz

Edit /etc/mkinitcpio.conf and add overlayroot to HOOKS array:

HOOKS=(base udev autodetect modconf block filesystems keyboard fsck overlayroot)

Rebuild initramfs:

mkinitcpio -P

Edit /etc/fstab to make /boot read-only at next boot:

/dev/mmcblk0p1  /boot   vfat    defaults,ro        0       0

Add overlayroot to the end of /boot/cmdline.txt and mount /dev/mmcblk0p2 read-only:

root=/dev/mmcblk0p2 ro rootwait console=ttyAMA0,115200 console=tty1 selinux=0 plymouth.enable=0 smsc95xx.turbo_mode=N dwc_otg.lpm_enable=0 kgdboc=ttyAMA0,115200 elevator=noop overlayroot

Now reboot:

reboot

When you login you will now see a warning that any changes the file system will be non-persistent. You can use the rwrootfs command to remount all file systems as read-write inside a chroot shell. After exiting the shell, the file systems will remain read-write until you next reboot.

System upgrades

For system upgrades it is recommended that you make the root and /boot file systems read-write in /boot/cmdline.txt and /etc/fstab.

To do so, run the rwrootfs command perform the following file edits:

  • /boot/cmdline: remove overlayroot and change ro to rw
  • /etc/fstab: change ro to rw for /boot

Then reboot. The root and /boot file systems will now be mounted read/write once more.